{% load i18n %}
<form role="form" method="post" action="{{request.prefix}}/execute/launch/exporttofolder/">{% csrf_token %}
  <table>
    <tr>
      <td style="vertical-align:top; padding-left:15px">
        <button type="submit" class="btn btn-primary" id="exporttofolder" value="{% trans "export"|capfirst %}">{% trans "export"|capfirst %}</button>
      </td>
      <td colspan='5' style="padding-left:15px">
        <p>{% trans "Exports the plan (purchase orders, distribution orders and manufacturing orders) as a set of CSV files." %}</p>
      </td>
    </tr>
    <tr>
      <td></td>
      <td style="padding-left:15px"><strong>{% trans 'file name'|capfirst %}</strong></td>
      <td><strong>{% trans 'size'|capfirst %}</strong></td>
      <td><strong>{% trans 'changed'|capfirst %}</strong></td>
      <td>
        <div class="btn btn-sm btn-primary" style="margin-bottom: 5px" data-bs-toggle="tooltip" data-bs-placement="top"
          data-bs-title="{% trans 'Download all files' %}" onClick="downloadExportFile(1)">
          <span class="fa fa-arrow-down"></span>
        </div>
      </td>
      <td>
        <div class="btn btn-sm btn-danger deletefile" style="margin-bottom:5px" data-bs-toggle="tooltip" data-bs-placement="top"
          data-bs-title="{% trans 'Delete all files' %}" onClick="deleteExportFile(1, {{filesexported}})">
          <span class="fa fa-close"></span>
        </div>
      </td>
    </tr>
    {% for j in filesexported %}
    <tr data-file="{{j.0}}">
      <td></td>
      <td style="padding-left:15px">{{j.0}}</td>
      <td>{{j.2}}</td>
      <td>{{j.1}}</td>
      <td>
        <div class="btn btn-sm btn-primary downloadfile" style="margin-bottom:5px;" data-bs-toggle="tooltip"
          data-bs-placement="top" data-bs-title="Download file" onClick="downloadExportFile(1, '{{j.0}}')">
          <span class="fa fa-arrow-down"></span>
        </div>
      </td>
      <td>
        <div class="btn btn-sm btn-danger deletefile" style="margin-bottom:5px" data-bs-toggle="tooltip"
          data-bs-placement="top" data-bs-title="Delete file from folder" onClick="deleteExportFile(1, '{{j.0}}')">
          <span class="fa fa-close"></span>
        </div>
      </td>
    </tr>
    {% endfor %}
  </table>
</form>
<script>
function deleteExportFile(folder, filename) {
  $.jgrid.hideModal("#searchmodfbox_grid");
  var dialogcontent;
  if (typeof filename === 'object') {
    dialogcontent = "{% trans 'You are about to delete all files' %}";
    var oldfilename = filename;
    filename = 'AllFiles';
  } else {
    dialogcontent = interpolate("{% trans 'You are about to delete file %s' %}", [filename]);
  }

  $("#popup").html('<div class="modal-dialog">'+
    '<div class="modal-content">'+
      '<div class="modal-header">'+
        '<h5 class="modal-title">{% trans 'Delete file' %}</h5>'+
      '</div>'+
      '<div class="modal-body"><p>'+
      dialogcontent +
      '</p></div>'+
      '<div class="modal-footer justify-content-between">'+
        '<input type="submit" id="cancelbutton" role="button" class="btn btn-primary pull-left" data-bs-dismiss="modal" value="{% trans 'Cancel' %}">'+
        '<input type="submit" id="confirmbutton" role="button" class="btn btn-danger float-end" value="{% trans 'Confirm' %}">'+
      '</div>'+
    '</div>'+
  '</div>' );
  showModal('popup');

  $('#confirmbutton').on('click', function() {
    $.ajax({
      url: "{{request.prefix}}/execute/deletefromfolder/" + folder + "/" + filename + "/",
      type: ("delete").toUpperCase(),
      success: function () {
        if (filename === 'AllFiles') {
          $("#popup .modal-body>p").text("{% trans 'All data files were deleted' %}");
        } else {
          $("#popup .modal-body>p").text(interpolate("{% trans 'File %s was deleted' %}", [filename]));
        }
        $('#confirmbutton').hide();
        $('#cancelbutton').attr('value', "{% trans 'Close' %}");
        $('#cancelbutton').one('click', function() {location.reload();});
      },
      error: function (result, stat, errorThrown) {
        if (result.status == 401) {
          location.reload();
          return;
        }
        var filelist = result.responseText.split(' / ');
        var elem = $("#popup .modal-body>p");
        if (filelist.length === 1) {
          elem.text(interpolate("{% trans 'File %s was not deleted' %}", [filename]));
        } else {
          for (var i = 1; i < filelist.length; i++) {
            if (i === 1) {
              elem.text(interpolate("{% trans 'File %s was not deleted' }", [filelist[i]]));
            } else {
              elem.parent().append('<p>'+interpolate("{% trans "File %s was not deleted" %}", [filelist[i]])+'</p>');
            }
          }
        }
        $("#popup .modal-body>p").addClass('alert alert-danger');
        $('#confirmbutton').hide();
        $('#cancelbutton').attr('value', "{% trans 'Close' %}");
        $('#cancelbutton').one('click', function() {$("#popup").hide();});
        }
    })
  })
}
function downloadExportFile(folder, filename) {
  $.jgrid.hideModal("#searchmodfbox_grid");
  if (typeof filename === 'undefined')
    window.open("{{request.prefix}}/execute/downloadfromfolder/" + folder + "/", '_blank');
  else
  	window.open("{{request.prefix}}/execute/downloadfromfolder/" + folder + "/" + filename + '/', '_blank');
}
</script>